package net.zjitc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Category;
import net.zjitc.entity.Workplace;
import net.zjitc.exception.CategoryException;
import net.zjitc.service.ICategoryService;
import net.zjitc.service.IWorkplaceService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @auther 郭梦婷
 * @create 2022-05-17 13:22:12
 * @describe 前端控制器
 */
@CrossOrigin
@RestController
@Api(tags = "地区管理")
public class WorkplaceController {
    @Reference
    private IWorkplaceService workplaceService;
    ResponseResult result=new ResponseResult();

    @ApiOperation("获取所有位置信息")
    @GetMapping("workplace")
    public ResponseResult getWorkplace(@RequestParam(required = false) Integer type,
                                        @RequestParam(required = false) Integer pagenum,
                                        @RequestParam(required = false) Integer pagesize) throws CategoryException {
        try {
            if (pagenum==null||pagesize==null){
                List<Workplace> listCWorkplace = workplaceService.getListWorkplace(type);
                if (listCWorkplace!=null){
                    result.setSuccess("获取成功",listCWorkplace);

                }
            }else {
                Page<Workplace> pageWorkplace = workplaceService.getPageWorkplace(type, pagenum, pagesize);
                if (pageWorkplace.getTotal()!=0){
                    result.setSuccess("获取成功",pageWorkplace);
                }
            }
        }catch (Exception e){
            result.setError("获取失败");
        }
        return result;
    }

    //添加
    @ApiOperation("添加地区")
    @PostMapping("workplace")
    public ResponseResult addcategories(@RequestBody Workplace Workplace){
        Workplace workplace = workplaceService.addLevel(Workplace);
        if (workplace!=null){
            result.setSuccess("添加地区成功",workplace);
        }else {
            result.setError("添加地区失败");
        }
        return result;
    }
    //根据id查询
    @ApiOperation("根据id查询职位")
    @GetMapping("workplace/{id}")
    public ResponseResult findCategoryById(@PathVariable(value = "id") Integer id){
        Workplace workplace=workplaceService.findById(id);
        if (workplace!=null){
            result.setSuccess("查询成功",workplace);
        }else {
            result.setError("查询失败");
        }
        return result;
    }
    //根据id修改
    @ApiOperation("修改职位")
    @PutMapping("workplace")
    public ResponseResult updateCategory(@RequestBody Workplace workplace){
        Workplace workplace1=workplaceService.updateByid(workplace);
        if (workplace1!=null){
            result.setSuccess("更新成功",workplace1);
        }else {
            result.setError("更新失败");
        }
        return result;
    }
    //根据id删除
    @ApiOperation("删除职位")
    @DeleteMapping("workplace/{id}")
    public ResponseResult deleteCategories(@PathVariable(value = "id") Integer id){
        Integer i=workplaceService.deleteById(id);
        if (i!=-1){
            result.setSuccess("删除成功",null);
        }else {
            result.setError("删除失败");
        }
        return result;
    }

}

